package com.hangsu.train.components.service.impl;


import com.hangsu.train.components.convert.PermissConvert;
import com.hangsu.train.components.convert.SecurityConvert;
import com.hangsu.train.components.dao.*;
import com.hangsu.train.components.dto.PermissDto;
import com.hangsu.train.components.dto.SecurityRespDto;
import com.hangsu.train.components.entity.Permiss;
import com.hangsu.train.components.entity.Role;
import com.hangsu.train.components.entity.User;
import com.hangsu.train.components.entity.UserRole;
import com.hangsu.train.components.service.SecurityService;
import com.hangsu.train.components.vo.MenuVo;
import com.hangsu.train.components.vo.UserRoleVo;
import com.hangsu.train.components.web.utils.PermissUtils;
import com.hangsu.train.core.keygen.KeyGenerate;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SecurityServiceImpl implements SecurityService {

    private PermissDao permissDao;
    private RoleDao roleDao;
    private UserRoleDao userRoleDao;
    private SecurityConvert securityConvert;
    private UserDao userDao;
    private RolePermissDao rolePermissDao;

    private PermissConvert permissConvert;

    private KeyGenerate keyGenerate;


    @Resource
    public void setPermissDao(PermissDao permissDao) {
        this.permissDao = permissDao;
    }

    @Resource
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Resource
    public void setSecurityConvert(SecurityConvert securityConvert) {
        this.securityConvert = securityConvert;
    }

    @Resource
    public void setUserRoleDao(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    @Resource
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Resource
    public void setRolePermissDao(RolePermissDao rolePermissDao) {
        this.rolePermissDao = rolePermissDao;
    }

    @Resource
    public void setPermissConvert(PermissConvert permissConvert) {
        this.permissConvert = permissConvert;
    }

    @Resource
    public void setKeyGenerate(KeyGenerate keyGenerate) {
        this.keyGenerate = keyGenerate;
    }

    @Override
    public List<MenuVo> getUserMenus(String userCode) {
        List<Role> roles = roleDao.getRolesByUserCode(userCode);
        if (roles == null || roles.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> roleIdList = roles.stream().map(Role::getId).collect(Collectors.toList());
        List<Permiss> permissions = permissDao.getMenus(roleIdList);
        List<Permiss> permissTree = PermissUtils.transToTree(permissions);
        return permissTree.stream().map(permission -> securityConvert.convert(permission)).collect(Collectors.toList());
    }

    @Override
    public SecurityRespDto getUserSecurity(String userCode) {

        return null;
    }

    @Override
    public  List<PermissDto> getSecurityList(String userCode) {
        return null;
    }

    @Override
    @Transactional
    public boolean userAuthz(List<String> roleCodes, String userCode) {
        userRoleDao.delUserRole(userCode);
        if (roleCodes != null && !roleCodes.isEmpty()) {
            User user = userDao.getUserByCode(userCode);
            List<Role> list = roleDao.getRoleByCodes(roleCodes);
            if (user != null && list != null && !list.isEmpty()) {
                List<UserRole> userRoleList = new ArrayList<>();
                for (Role role : list) {
                    UserRole ur = new UserRole();
                    ur.setId((Long) keyGenerate.generateKey());
                    ur.setRoleCode(role.getCode());
                    ur.setRoleId(role.getId());
                    ur.setUserCode(user.getUserCode());
                    ur.setUserId(user.getId());
                    userRoleList.add(ur);
                }
                userRoleDao.insertBatch(userRoleList);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Permiss> getUserPermiss(String userCode) {
        List<Role> roles = roleDao.getRolesByUserCode(userCode);
        if (roles == null || roles.isEmpty()) {
            return new ArrayList<>();
        }
        List<Long> roleIdList = roles.stream().map(Role::getId).collect(Collectors.toList());
        return permissDao.getMenus(roleIdList);
    }

    @Override
    public List<UserRoleVo> getUserRoles(String userCode) {
        List<Role> roles = roleDao.getRolesByUserCode(userCode);
        List<String> roleCodes = roles.stream().map(Role::getCode).collect(Collectors.toList());
        List<Role> roleList = roleDao.getAll();
        return securityConvert.convert(roleList, roleCodes);
    }

}